<template>
  <div class="main_spac1 maincl">
    <div class="main_white padding10">
      <div class="mainor_form">
        <div class="mainor_te">
          <router-link to="/purchasing/return/add" class="mainor_tea"
            >创建退货单</router-link
          >
        </div>
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="userListFrom"
          label-width="70px"
        >
          <el-form-item label="采购方式" prop="mode">
            <el-select
              v-model.trim="form.mode"
              placeholder="采购方式"
              class="wid220"
            >
              <el-option label="所有" value=""></el-option>
              <el-option
                v-for="(modeList, index) in list.modeList"
                :key="index"
                :label="modeList"
                :value="parseInt(index)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="采购员" prop="buyer_id">
            <el-select
              v-model.trim="form.buyer_id"
              placeholder="采购员"
              class="wid220"
            >
              <el-option label="所有" value=""></el-option>
              <el-option
                v-for="(buyer, index) in list.buyer"
                :key="index"
                :label="buyer"
                :value="parseInt(index)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="供应商" prop="supplier_id">
            <el-select
              v-model.trim="form.supplier_id"
              placeholder="供应商"
              class="wid220"
            >
              <el-option label="所有" value=""></el-option>
              <el-option
                v-for="(supplier, index) in list.supplier"
                :key="index"
                :label="supplier"
                :value="parseInt(index)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="单据状态" prop="status">
            <el-select
              v-model.trim="form.status"
              placeholder="单据状态"
              class="wid220"
            >
              <el-option label="所有" value=""></el-option>
              <el-option
                v-for="(statusList, index) in list.statusList"
                :key="index"
                :label="statusList"
                :value="parseInt(index+1)"
              ></el-option>
            </el-select>
          </el-form-item>
          <div class="more el-form-item" @click="more">
            <span>更多</span>
            <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
          </div>
          <div class="clearfix"></div>
          <el-collapse-transition>
            <div v-if="moredata">
              <el-form-item label="退货单号" prop="order_sn">
                <el-input
                  v-model.trim="form.order_sn"
                  placeholder="退货单号"
                  clearable
                  class="wid220"
                ></el-input>
              </el-form-item>
              <el-form-item label="采购单号" prop="purchase_order_sn">
                <el-input
                  v-model.trim="form.purchase_order_sn"
                  placeholder="采购单号"
                  clearable
                  class="wid220"
                ></el-input>
              </el-form-item>
              <el-form-item label="退货方式" prop="type">
                <el-select
                  v-model.trim="form.type"
                  placeholder="退货方式"
                  class="wid220"
                >
                  <el-option label="所有" value=""></el-option>
                  <el-option
                    v-for="(typeList, index) in list.typeList"
                    :key="index"
                    :label="typeList"
                    :value="parseInt(index)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="创建日期" prop="createTime">
                <el-date-picker
                  v-model="createTime"
                  type="daterange"
                  class="time220"
                  :editable="false"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="yyyy-MM-dd"
                  :clearable="false"
                  :picker-options="pickerOptions"
                />
              </el-form-item>
              <el-form-item label="审核日期" prop="establishTime">
                <el-date-picker
                  v-model="establishTime"
                  type="daterange"
                  class="time220"
                  :editable="false"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="yyyy-MM-dd"
                  :clearable="false"
                  :picker-options="pickerOptions"
                />
              </el-form-item>
            </div>
          </el-collapse-transition>
          <div class="clearfix"></div>
          <el-form-item class="marginTop10">
            <el-button
              class="reset marginLeft10"
              type="primary"
              @click="
                $refs['userListFrom'].resetFields(),
                  (createTime = []),
                  (establishTime = [])
              "
              >重置</el-button
            >
            <el-button
              type="primary"
              @click="getArticleList"
              :loading="listLoading"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="main_table" style="min-height: 570px">
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list.list.data"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
        >
          <el-table-column
            label="退货单号"
            prop="order_sn"
            width="160"
            :show-overflow-tooltip="true"
          />
          <el-table-column></el-table-column>
          <el-table-column
            label="采购单号"
            prop="order_sn"
            width="160"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">{{ scope.row.purchase?scope.row.purchase.order_sn:'' }}</template>
          </el-table-column>
          <el-table-column></el-table-column>
          <el-table-column
            label="采购方式"
            prop="byname"
            width="120"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">{{ list.modeList[scope.row.mode] }}</template>
          </el-table-column>
          <el-table-column></el-table-column>
          <el-table-column
            label="采购员/供应商"
            prop="market"
            width="180"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">{{ scope.row.mode == 1
                  ? list.buyer[scope.row.buyer_id]
                  : list.supplier[scope.row.supplier_id] }}</template>
          </el-table-column>
          <el-table-column
            label="退货金额"
            prop="money"
            width="120"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="单据状态"
            prop="status"
            width="120"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">{{ list.statusList[scope.row.status] }}</template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            prop="created_at"
            width="120"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ parseTime(scope.row.created_at, "{y}-{m}-{d}") }}
            </template>
          </el-table-column>
          <el-table-column
            label="退货时间"
            prop="created_at"
            width="120"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ scope.row.return_time?parseTime(scope.row.return_time, "{y}-{m}-{d}"):'' }}
            </template>
          </el-table-column>
          <el-table-column
            label="管理操作"
            align="center"
            fixed="right"
            width="200"
            class-name="main_hei"
          >
            <template slot-scope="scope">
              <router-link :to="'/purchasing/return/edit/' + scope.row.id">
                <el-link type="warning" v-if="scope.row.status == 0"
                  >审核</el-link
                >
              </router-link>
              <router-link :to="'/purchasing/return/see/' + scope.row.id">
                <el-link type="success">查看</el-link>
              </router-link>
              <el-link type="danger" @click="delet(scope.$index, scope.row.id)" v-if="scope.row.status==0"
                >取消</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="main_page boxdq">
        <div></div>
        <!-- 分页 -->
        <div class="main_page_r">
          <el-pagination
            :current-page="current.page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            background
            @size-change="changeLimit"
            @current-change="changePage"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { purchaseReturn_index, purchaseReturn_cancel } from "@/api/purchasing/purchasingReturn";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";
import dateOption from "@/options/dateOption";

export default {
  data() {
    return {
      // 时间
      parseTime,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 搜索
      form: {
        return_start_time: "",
        return_end_time: "",
        start_time: "",
        end_time: "",
        purchase_order_sn: "",
        type: null,
        order_sn: "",
        mode: null,
        status: null,
        buyer_id: null,
        supplier_id: null,
      },
      createTime: [],
      establishTime: [],
      pickerOptions: dateOption,
      // 更多
      moredata: false,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      listLoading: false,
    };
  },
  methods: {
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.purchaseReturn_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.purchaseReturn_index();
    },
    changePage(page) {
      this.current.page = page;
      this.purchaseReturn_index();
    },
    // 取消
    delet(index, id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id}
      );
      this.$confirm("是否取消退货单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          purchaseReturn_cancel(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "取消成功",
                type: "success",
              });
              this.purchaseReturn_index();
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 列表
    purchaseReturn_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      purchaseReturn_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";
      form.return_start_time = this.establishTime[0]
        ? this.establishTime[0]
        : "";
      form.return_end_time = this.establishTime[1] ? this.establishTime[1] : "";
      
      return form;
    },
  },
  created() {
    // 列表
    this.purchaseReturn_index();
  },
};
</script>
<style lang="scss" scoped>
.mainor_form form {
  width: calc(100% - 140px);
}
</style>
